package com.gidgabs.modules.api.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.gidgabs.common.annotation.AuthIgnoreApi;
import com.gidgabs.common.annotation.LoginUserApi;
import com.gidgabs.common.utils.PageUtils;
import com.gidgabs.common.utils.Query;
import com.gidgabs.common.utils.R;
import com.gidgabs.common.utils.ReturnJson;
import com.gidgabs.common.validator.Assert;
import com.gidgabs.dto.TbProductCommentDto;
import com.gidgabs.modules.api.service.TbProductCommentService;
import com.gidgabs.modules.api.service.TbUserService;
import com.gidgabs.modules.entity.TbProductCommentEntity;
import com.gidgabs.modules.entity.TbUser;
import com.gidgabs.utils.SessionDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 产品关系表-评论表
 *
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-06-29 17:16:10
 */
@Api("产品评论功能模块")
@RestController
@RequestMapping("/api/productcomment")
public class TbProductCommentController {

    @Autowired
    private TbProductCommentService productCommentService;

    @Autowired
    private TbUserService userService;

    /**
     * 根据产品id 查询评论列表（分页）同时获取是否被当前用户点赞
     *
     * @param pageSize
     * @param pageNo
     * @param productId
     * @return
     */
    @ApiOperation(value = "根据产品id 查询评论列表（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "页面大小", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "productId", value = "产品id", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    @PostMapping("/list")
    public ReturnJson<TbProductCommentDto> list(Integer pageSize, Integer pageNo, Integer productId) {
        //获取用户信息
        TbUser tbUser = new TbUser();
        tbUser.setUserId(SessionDataUtil.getCurrentUserId());
        TbUser user = userService.queryObject(tbUser);
        if (user == null) {
            return ReturnJson.fail("登录用户不存在！");
        }
        Assert.isNull(productId, "不能为空");
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        return productCommentService.queryListBy(pageSize, pageNo, productId, user);
    }

    /**
     * 添加评论
     *
     * @param productCommentDto
     * @return
     */
    @ApiOperation(value = "添加评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    @PostMapping("/save")
    public ReturnJson<TbProductCommentDto> save(@ModelAttribute TbProductCommentDto productCommentDto) {
        //获取用户信息
        TbUser tbUser = new TbUser();
        tbUser.setUserId(SessionDataUtil.getCurrentUserId());
        TbUser user = userService.queryObject(tbUser);
        if (user == null) {
            return ReturnJson.fail("登录用户不存在！");
        }
        //数据转换
        TbProductCommentEntity tbProductCommentEntity = new TbProductCommentEntity();
        tbProductCommentEntity.setUserIdMaster(user.getUserId());
        tbProductCommentEntity.setCommentIdSlave(productCommentDto.getCommentIdSlave());
        tbProductCommentEntity.setCreateTime(new Date());
        tbProductCommentEntity.setValue(productCommentDto.getValue());
        productCommentService.save(tbProductCommentEntity);
        return ReturnJson.success("添加成功");
    }

}
